package com.openbravo.pos.panels.link;

import javax.swing.JPanel;

import com.openbravo.bean.Product;
import com.openbravo.bean.sales.QuickProductExt;
import com.openbravo.data.basic.BasicException;
import com.openbravo.data.gui.MessageInf;
import com.openbravo.data.loader.query.QBFCompareEnum;
import com.openbravo.data.loader.query.QBFParameter;
import com.openbravo.data.loader.query.QBFParameters;
import com.openbravo.pos.panels.QuickTimerFilter;
import com.openbravo.pos.panels.event.TimeCycleChangedEvent;
import com.openbravo.pos.panels.event.TimeCycleChangedListener;
import com.openbravo.pos.panels.link.model.QuickProductExtTableModel;

import e.odbo.data.bean.IKeyed;
import cn.ekuma.epos.datalogic.I_DataLogicSales;
import cn.ekuma.epos.db.table.pos.I_QuickProduct;
import cn.ekuma.util.SwingUtil;


public class JQuickProductPanel extends AbstractProductSelectTablePanel<QuickProductExt> implements  TimeCycleChangedListener{

	QuickTimerFilter timerFilter;
	QBFParameters qbfParameters;
	QBFParameter startDate,endDate;
	
	public JQuickProductPanel(I_DataLogicSales dlSales) {
		super(dlSales, new QuickProductExtTableModel());
		qbfParameters=new QBFParameters();
		startDate=new QBFParameter(I_QuickProduct.CURDATE);
		endDate=new QBFParameter(I_QuickProduct.CURDATE);
		qbfParameters.and(startDate).and(endDate);
		timerFilter.setDefineSelected(QuickTimerFilter.TIMERCYCLETYPE_Today);
	}

	@Override
	public String getProductId(QuickProductExt obj){
		return obj.getProductId();
	}

	@Override
	public JPanel getToolBar() {
	    timerFilter=new QuickTimerFilter();
		timerFilter.addTimeCycleChangedListener(this);
		return timerFilter;
	}


	@Override
	public void baseBeanChanged(IKeyed<String> key, Product obj) {

	}	
	

	@Override
	public void baseBeanChanged(Product o) {
	
	}

	@Override
	public void timeCycleChanged(TimeCycleChangedEvent e) {
		startDate.setQbf((e.getStartDate()== null)? QBFCompareEnum.COMP_NONE: QBFCompareEnum.COMP_GREATEROREQUALS);
		startDate.setValue(e.getStartDate());
	    endDate.setQbf(( e.getEndDate() == null) ? QBFCompareEnum.COMP_NONE: QBFCompareEnum.COMP_LESS);
	    endDate.setValue(e.getEndDate());
		try {
			tableModel.resert(dlSales.query(QuickProductExt.class, qbfParameters));
			SwingUtil.PreferredJXTableColumn(jXTable1);
		} catch (BasicException ex) {
				new MessageInf(ex).show(this);
		}
		
	}
}